package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.system.domain.LatitudeAndLongitude;
import com.ruoyi.system.domain.TourismPact;
import com.ruoyi.system.domain.TourismUser;
import com.ruoyi.system.mapper.LatitudeAndLongitudeMapper;
import com.ruoyi.system.mapper.TourismPactMapper;
import com.ruoyi.system.mapper.TourismUserMapper;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TourismSpotMapper;
import com.ruoyi.system.domain.TourismSpot;
import com.ruoyi.system.service.ITourismSpotService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 景点Service业务层处理
 *
 * @author gqd
 * @date 2020-12-25
 */
@Service
public class TourismSpotServiceImpl implements ITourismSpotService
{
    @Autowired
    private TourismSpotMapper tourismSpotMapper;
    @Resource
    private TourismPactMapper tourismPactMapper;
    @Resource
    private TourismUserMapper tourismUserMapper;
    @Resource
    private LatitudeAndLongitudeMapper latitudeAndLongitudeMapper;

    /**
     * 查询景点
     *
     * @param id 景点ID
     * @return 景点
     */
    @Override
    public TourismSpot selectTourismSpotById(Integer id)
    {
        return tourismSpotMapper.selectTourismSpotById(id);
    }

    /**
     * 查询景点列表
     *
     * @param tourismSpot 景点
     * @return 景点
     */
    @Override
    public List<TourismSpot> selectTourismSpotList(TourismSpot tourismSpot)
    {
        return tourismSpotMapper.selectTourismSpotList(tourismSpot);
    }

    /**
     * 新增景点
     *
     * @param tourismSpot 景点
     * @return 结果
     */
    @Override
    public int insertTourismSpot(TourismSpot tourismSpot)
    {
        tourismSpot.setCreateTime(DateUtils.getNowDate());
        return tourismSpotMapper.insertTourismSpot(tourismSpot);
    }

    /**
     * 修改景点
     *
     * @param tourismSpot 景点
     * @return 结果
     */
    @Override
    public int updateTourismSpot(TourismSpot tourismSpot)
    {
        tourismSpot.setUpdateTime(DateUtils.getNowDate());
        return tourismSpotMapper.updateTourismSpot(tourismSpot);
    }

    /**
     * 批量删除景点
     *
     * @param ids 需要删除的景点ID
     * @return 结果
     */
    @Override
    public int deleteTourismSpotByIds(Integer[] ids)
    {
        return tourismSpotMapper.deleteTourismSpotByIds(ids);
    }

    /**
     * 删除景点信息
     *
     * @param id 景点ID
     * @return 结果
     */
    @Override
    public int deleteTourismSpotById(Integer id)
    {
        return tourismSpotMapper.deleteTourismSpotById(id);
    }

    /**
     * 精选景点的查询
     * @param spotName
     * @return
     */
    @Override
    public AjaxResult selectSpotByName(String spotName) {
        List<TourismSpot> tourismSpots = tourismSpotMapper.selectSpotByName(spotName);
        if(tourismSpots.size() == 0 || tourismSpots == null){
            return AjaxResult.error("暂时还没有精选景点的信息");
        }
        return AjaxResult.success(tourismSpots);
    }

    /**
     * 根据景点id查询景点详细信息
     * @param spotId
     * @return
     */
    @Override
    public AjaxResult selectSpotById(Integer spotId, Integer userId) {
        TourismUser tourismUser = tourismUserMapper.selectByUserId(userId);

        TourismSpot tourismSpot = tourismSpotMapper.selectSpotById(spotId);
        if(tourismSpot == null){
            return AjaxResult.error("发生错误，暂时没有景点信息");
        }
        LatitudeAndLongitude latitudeAndLongitude = latitudeAndLongitudeMapper.selectIt();
        GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(latitudeAndLongitude.getLatone()), Double.parseDouble(latitudeAndLongitude.getLngone()));
        GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(latitudeAndLongitude.getLattwo()), Double.parseDouble(latitudeAndLongitude.getLngtwo()));
        double meter2 = LogUtils.getDistanceMeter(source, target, Ellipsoid.WGS84);
        int i = new Double(meter2).intValue()/1000;
        tourismSpot.setDistance(i);
        if(tourismUser == null || userId == null){
            tourismSpot.setFlag("0");
        }else{
            TourismPact tourismPact = tourismPactMapper.selectSpotPact(spotId,userId);
            if(tourismPact == null){
                tourismSpot.setFlag("0");
            }else {
                tourismSpot.setFlag("1");
            }
        }
        return AjaxResult.success(tourismSpot);
    }
}
